// 项目中的中间件
let express = require('express');
let app = express();

/* 解决跨域 */
app.all('*', function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*'); // 任何网址都可以访问
    res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS'); // 允许请求的方式
    res.header('Access-Control-Allow-Headers', 'X-Requested-With'); //判断请求方式
    res.header('Content-Type', 'application/json;charset=utf-8'); // 内容
    next();
});

app.get('/search', (req, res) => {
    let _serchKey = req.query.search_key;
    let _tempGoods = [, {
            name: '好看1男装',
            price: 99
        },
        {
            name: '实惠男装',
            price: 97
        },
        {
            name: '可爱的童装',
            price: 33
        }, {
            name: '漂亮的连衣裙',
            price: 11
        }, {
            name: '男士短裤',
            price: 44
        }, {
            name: '女士长裙',
            price: 88
        }
    ];

    // filter 只返回结果true
    let _filterGoods = _tempGoods.filter(r => {
        return r.name.indexOf(_serchKey) !== -1
    });
    let sed = [{ name: '没结果', price: '' }]
    let resultObj = _filterGoods.length > 0 ? _filterGoods : sed
    res.send(resultObj);
});

app.get('/xxx', (req, res) => {
    res.send('xxxxxxx')
});

app.get('/get_tabBtn_list', (req, res) => {
    let _d = ['推荐', '居家生活', '服饰鞋包', '美食酒水', '个护清洁', '母婴亲子', '运行旅行', '数码家电', '全球特色'];
    res.send(_d);
});


app.get('/get_swipe_img', (req, res) => {
    let _n = [
        'https://yanxuan.nosdn.127.net/51dbe6f81d9bf92df9858e95929eedb9.jpg?type=webp&imageView&quality=75&thumbnail=750x0',
        'https://yanxuan.nosdn.127.net/2d75753a4e63121e7911801b2c0debba.jpg?type=webp&imageView&quality=75&thumbnail=750x0',
        'http://yanxuan-miaobi.nos-jd.163yun.com/1572025_1_5_wap_727631487696e9635e5d3e33371b20f9.jpg?type=webp&imageView&quality=75&thumbnail=750x0',
        'https://yanxuan.nosdn.127.net/62e05f28582b83beb000a5f0afe03cc5.jpg?type=webp&imageView&quality=75&thumbnail=750x0',
        'https://yanxuan.nosdn.127.net/b360a9d953e2eb1a1e30068040cf7435.jpg?type=webp&imageView&quality=75&thumbnail=750x0',
    ];
    res.send(_n);
});

// 分类商品列表
app.get('/cate_goods_list_0', (req, res) => {
    let _d = [{
        goods_img: 'https://yanxuan-item.nosdn.127.net/8e993243c58d5798add86c2d62de82b8.png',
        goods_name: '空气循环扇+手持小风扇（清凉套装）',
        goods_price: 679
    }, {
        goods_img: 'https://yanxuan-item.nosdn.127.net/ebdd92a84e2c1b8eccba83eada94886d.png',
        goods_name: '【预付立省56元】全价猫粮 1.8千克*5袋',
        goods_price: 159
    }, {
        goods_img: 'https://yanxuan-item.nosdn.127.net/033aca51607876112883541926cfd1e1.png',
        goods_name: '怎么睡都舒适 泰国93%天然乳胶床垫5cm7.5cm',
        goods_price: 189
    }];

    res.send(_d);
});

app.get('/cate_goods_list_1', (req, res) => {
    let _d = [{
        goods_img: 'https://yanxuan-item.nosdn.127.net/a353330db78461e51bccf6cb01ee81f8.png',
        goods_name: '久吹不头疼 德国负离子净化无叶风扇',
        goods_price: 578
    }, {
        goods_img: 'https://yanxuan-item.nosdn.127.net/51f227659459a30593fb40508729db08.png',
        goods_name: '凉而不冰，平滑密编清凉仿藤席 三件套',
        goods_price: 209
    }, {
        goods_img: 'https://yanxuan-item.nosdn.127.net/3f4b684dad28eafd39b10af4edec40b2.png',
        goods_name: '【预付立省30元】纯牛奶酸牛奶12盒*4提',
        goods_price: 349
    }, {
        goods_img: 'https://yanxuan-item.nosdn.127.net/10b6d8a93eab0984f388f15a4044da8e.png',
        goods_name: '玻璃光洁无瑕，全自动智能擦窗机器人',
        goods_price: 49.9
    }];
    res.send(_d);
});

app.get('/cate_goods_list_2', (req, res) => {
    let _d = [{
        goods_img: 'https://yanxuan-item.nosdn.127.net/8959f0bbb0d4fe8d5804f323b65e2d90.png',
        goods_name: '澳大利亚深海鱼油胶囊 365粒*2瓶 送1箱牛奶',
        goods_price: 109
    }, {
        goods_img: 'https://yanxuan-item.nosdn.127.net/18c025effd3d8a7ee15e9fc67c285a98.png',
        goods_name: '不弯腰不脏手，洗脱一体懒人拖把灵巧款',
        goods_price: 99
    }, {
        goods_img: 'https://yanxuan-item.nosdn.127.net/855a7ac9f14f9bc7521f4f84673165d7.png',
        goods_name: '新西兰全脂鲜奶粉 1kg*2袋',
        goods_price: 19
    }, {
        goods_img: 'https://yanxuan-item.nosdn.127.net/1c91625453b3e5b15c769b09af4a1087.png',
        goods_name: '光感密集焕亮精华液 2瓶囤货装',
        goods_price: 49.9
    }, {
        goods_img: 'https://yanxuan-item.nosdn.127.net/7a09bca04a51a269263061db5fcd0ba4.png',
        goods_name: '【预付立省120元】鲜萃每日坚果 3盒装',
        goods_price: 49.9
    }, {
        goods_img: 'https://yanxuan-item.nosdn.127.net/3f4b684dad28eafd39b10af4edec40b2.png',
        goods_name: '【预付立省30元】纯牛奶酸牛奶12盒*4提',
        goods_price: 349
    }, {
        goods_img: 'https://yanxuan-item.nosdn.127.net/10b6d8a93eab0984f388f15a4044da8e.png',
        goods_name: '玻璃光洁无瑕，全自动智能擦窗机器人',
        goods_price: 49.9
    }];
    res.send(_d);
});

app.listen(3344, () => {
    console.log('监听端口3344');
});